package com.xiongtian.springframework.context.annotation;

import cn.hutool.core.util.ClassUtil;
import com.xiongtian.springframework.beans.factory.config.BeanDefinition;
import com.xiongtian.springframework.stereotype.Component;

import java.util.LinkedHashSet;
import java.util.Set;

/**
 * @Author: xiongtian
 * @CreateTime: 2025-04-24  16:46
 * @Version: 1.0
 * @Description: 类路径扫描器
 * 扫描指定包下的类，将符合条件的类封装为 BeanDefinition，注册到 BeanDefinitionRegistry 中
 * 符合条件的类：
 * 1. 被 @Component 注解标注的类
 */

public class ClassPathScanningCandidateComponentProvider {

    /**
     * 扫描制定路径下，所有@Component注解的类
     * 封装为BeanDefinition
     *
     * @param basePackage 指定路径
     * @return BeanDefinition集合
     */
    public Set<BeanDefinition> findCandidateComponents(String basePackage) {
        Set<BeanDefinition> candidates = new LinkedHashSet<>();
        Set<Class<?>> classes = ClassUtil.scanPackageByAnnotation(basePackage, Component.class);
        for (Class<?> clazz : classes) {
            candidates.add(new BeanDefinition(clazz));
        }
        return candidates;
    }
}
